<!--
 * @description  : 案件-案件移交
 * @descriptionDetail: 案件-案件移交
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-08-25 15:43:11
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="transfer">
      <u-form :model="form" label-width="auto" style="padding: 0px 15px" ref="uForm">
        <u-form-item :required="true" label="单位类型" prop="unitType" class="row">
          <dic-input
            :keyWord="$u.dic.DIC_KEYS.C_FEEDBACK_ORG_TYPE"
            :dicCode="form.transOrgType"
            :border="false"
            input-align="right"
            type="select"
            :select-open="dicSelectOpen($u.dic.DIC_KEYS.C_FEEDBACK_ORG_TYPE)"
            placeholder="请选择单位类型"
            v-model="form.unitType"
            @changeInputModelData="
              (name) => {
                $set(form, 'unitType', name);
              }
            "
            @click="
              () => {
                if (state.canEdit) showDic($u.dic.DIC_KEYS.C_FEEDBACK_ORG_TYPE, form.reportType);
              }
            "
          />
        </u-form-item>
        <u-form-item :required="true" label="单位名称" label-position="top" prop="transOrg" class="row">
          <u-input
            class="over-height"
            :disabled="!state.canEdit"
            placeholder="请填写单位名称"
            v-model="form.transOrg"
          />
        </u-form-item>
        <u-form-item :required="true" label="移送函文号" label-position="top" prop="fullDocNo" class="row">
          <u-input
            class="over-height"
            :disabled="!state.canEdit"
            placeholder="请填写移送函文号"
            v-model="form.fullDocNo"
          />
        </u-form-item>
        <u-form-item :required="true" label="当事人" prop="party" class="row">
          <u-input
            class="over-height"
            :disabled="!state.canEdit"
            input-align="right"
            placeholder="请填写当事人"
            v-model="form.party"
          />
        </u-form-item>
        <u-form-item :required="true" label="案件性质" label-position="top" prop="casePropName" ref="caseChangeRef">
          <u-input
            type="textarea"
            class="over-height"
            :auto-height="true"
            placeholder="请选择"
            v-model="form.casePropName"
            @click="
              () => {
                if (state.canEdit) causeOfActionHandle();
              }
            "
          />
        </u-form-item>
        <u-form-item :required="true" label="案件材料" prop="dataNum" class="row">
          <u-input
            class="over-height"
            placeholder="请填写案件材料件数"
            :disabled="!state.canEdit"
            input-align="right"
            v-model="form.dataNum"
          /> 份
        </u-form-item>
        <u-form-item :required="true" label="移送财务清单" prop="pageNum" class="row">
          <u-input
            class="over-height"
            placeholder="请填写移送账务清单页数"
            :disabled="!state.canEdit"
            input-align="right"
            v-model="form.pageNum"
          /> 页
        </u-form-item>
      </u-form>
      <!-- 下拉选择用 -->
      <dic-choice
        :keyWord="dicKeyWords"
        :currentSelect="dicCurrentSelect"
        v-model="shapeShow"
        @confirm="selectDicConfirm"
      ></dic-choice>
    </view>
    <view class="bottom-btn-info" v-show="!!state.canEdit">
      <u-row gutter="16">
        <u-col span="6">
          <u-button plain type="primary" @click="submitBtn('save')">
            保存
          </u-button>
        </u-col>
        <u-col span="6">
          <u-button type="primary" @click="submitBtn('submit')">
            提交
          </u-button>
        </u-col>
      </u-row>
    </view>
  </view>
</template>

<script>
import DicInput from '@/components/dic_input.vue';
import DicChoice from '@/components/dic_choice.vue';
import caseService from '@/service/case/case.service';
import caseCommon from '../caseCommonMixin.js';

export default {
  components: {
    DicInput,
    DicChoice,
  },
  mixins: [caseCommon,],
  data() {
    return {
      caseParam: {}, // 上个页面传过来的案件信息
      isAction: false,
      show: false,
      showModel: false,
      shapeShow: false, // 举报形式 select
      dicType: '',
      dicKeyWords: '',
      dicCurrentSelect: '',
      // 案件移交表单
      form: {
        unitType: '',
        transOrg: '',
        fullDocNo: '',
        party: '',
        casePropName: '',
        dataNum: '',
        pageNum: '',
        transOrgType: '',
      },
      rules: {
        casePropName: [{
          required: true,
          message: '请选择案件性质',
          trigger: ['blur', 'change',],
        },],
        transOrg: [{
          required: true,
          message: '请填写单位名称',
          trigger: ['blur',],
        },],
        fullDocNo: [{
          required: true,
          message: '请填写移送函文号',
          trigger: ['blur',],
        },],
        party: [{
          required: true,
          message: '请填写当事人',
          trigger: ['blur',],
        },],
        dataNum: [{
          validator: (rule, value, callback) => {
            return this.$u.test.empty(value) ? false : true;
          },
          message: '请填写案件材料份数',
          trigger: ['blur',],
        },],
        pageNum: [{
          validator: (rule, value, callback) => {
            return this.$u.test.empty(value) ? false : true;
          },
          message: '请填写移送财务清单页数',
          trigger: ['blur',],
        },],
      },
    };
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  onLoad() {
    this.caseParam = this.getRouteParam();
  },
  watch: {
    isAction() {
      this.$refs.caseChangeRef.validation();
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      // 准备数据
      if (this.caseParam.bizUuid) {
        caseService.getSourceDocIns(this.caseParam.bizUuid).then((res) => {
          if (res.success) {
            this.caseParam.id = res.bean;
            this.initDocInfo();
          }
        });
      } else {
        this.initDocInfo();
      }
    },
    initDocInfo() {
      this.docStep((_doc) => {
        caseService.getTransIn(this.caseParam).then((res) => {
          // 有2个元素 说明是添加类型的
          if (res.success && res.data.length == 2) {
            // 取第二个
            let rs = res.data[1];
            for (let k in rs) {
              if (Object.prototype.hasOwnProperty.call(this.form, k)) {
                this.form[k] = rs[k];
              } else {
                this.$set(this.form, k, rs[k]);
              }
            }
          } else {
            let rs = res.data[0];
            // 说明已经有数据了
            for (let k in rs) {
              if (Object.prototype.hasOwnProperty.call(this.form, k)) {
                this.form[k] = rs[k];
              } else {
                this.$set(this.form, k, rs[k]);
              }
            }
          }
          this.$modal.hideLoading();
        });
      });
    },
    openModel() {
      this.showModel = true;
    },
    dicSelectOpen(keyWords) {
      if (this.dicKeyWords == keyWords && this.shapeShow) {
        return true;
      }
      return false;
    },
    showDic(key, cur) {
      this.dicKeyWords = key;
      this.dicCurrentSelect = cur;
      this.shapeShow = true;
    },

    /**
			 * @description 下拉选中确定事件，处理选中的结果绑定至数据
			 * @params e 选中的结果数据
			 */
    selectDicConfirm(e) {
      if (this.dicKeyWords == this.$u.dic.DIC_KEYS.C_FEEDBACK_ORG_TYPE) {
        // 未办证原因
        this.form.unitType = e[0].label;
        this.form.transOrgType = e[0].extra.bcbCode;
      }
    },
    // 案件性质点击事件
    causeOfActionHandle() {
      this.isAction = true;
      this.$u.route('pages/case/components/casePropertySelection');
    },
    // 选择案件性质返回的数据
    updateBackState(param) {
      if (param.caseSelectionData) {
        let [names, ids,] = [
          [],
          [],
        ];
        param.caseSelectionData.forEach((item) => {
          names.push(item.caseProperty);
          ids.push(item.casepropUuid);
        });
        this.form.transPropertyRelList = param.caseSelectionData;
        this.$set(this.form, 'casePropName', names.join(','));
        this.$set(this.form, 'caseProp', ids.join(','));
        this.isAction = false;
      }
    },

    submitBtn(isSubmit) {
      this.$refs.uForm.validate((valid) => {
        if (!valid) return;
        this.$modal.showLoading('保存中...');
        caseService
          .saveTransIn(this.form)
          .then((dr) => {
            this.$modal.hideLoading();
            if (!dr.success) {
              this.$modal.alert('保存失败:' + dr.msg);
              return;
            }
            this.form.transUuid = dr.bean.transUuid;
            this.form.cluesUuid = dr.bean.cluesUuid;
            if (isSubmit == 'submit') {
              this.$modal.showLoading('提交中...');
              caseService
                .submitTransIn(this.form, dr.data[0].docUuid, dr.data[0].docInsUuid)
                .then((sdr) => {
                  this.$modal.hideLoading();
                  this.$modal.alert('提交成功', () => {
                    this.backPrev({
                      paramKey: 'refreshBack',
                      param: true,
                    });
                  });
                })
                .catch((error) => {
                  this.$modal.hideLoading();
                  this.$modal.alert('保存失败:' + error);
                  console.error(error);
                });
            } else {
              this.$modal.alert('保存成功', () => {
                this.backPrev({
                  paramKey: 'refreshBack',
                  param: true,
                });
              });
            }
          })
          .catch((err) => {
            this.$modal.hideLoading();
            this.$modal.alert('保存失败:' + err);
            console.error(err);
          });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
	@import '../case_common.scss';

	.transfer {
		position: relative;
		width: 100%;

		.row {
			border-bottom: 2rpx solid $border-color-dark;
		}
	}
</style>
